#!/usr/bin python
# -*- coding: utf-8 -*-
# @Time    : 2023/12/21 16:25
# @Author  : hhr
import os
from openpyxl import Workbook
from openpyxl.styles import Alignment, Font
from openpyxl.styles import Border, Side



class DownloadPublishTheResults():
    def download_medical_ethics(self, datas, years):
        wb = Workbook()
        ws = wb['Sheet']
        ws.title = 'Sheet1'
        ws.merge_cells('A1:H1')
        ws.row_dimensions[1].height = 31
        top_left_cell = ws['A1']
        top_left_cell.value = f"{years}年医德医风考核结果公示"
        top_left_cell.font = Font(b=True, color="000000", size=18)
        top_left_cell.alignment = Alignment(horizontal="center", vertical="center")
        ws['A1'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))

        line_list = ["A2", "B2", "C2", "D2", "E2", "F2", "G2", "H2"]
        ws['A2'] = '序号'
        ws['B2'] = '参评科室/小组'
        ws['C2'] = '参评人数'
        ws['D2'] = '优秀'
        ws['E2'] = '优秀人数'
        ws['F2'] = '良好'
        ws['G2'] = '一般'
        ws['H2'] = '较差'
        ws.row_dimensions[2].height = 31
        for line in line_list:
            top_left_cell = ws[line]
            top_left_cell.font = Font(b=True, color="000000", size=12)
            top_left_cell.alignment = Alignment(horizontal="center", vertical="center")
            top_left_cell.border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
        row = 3
        for x in datas:
            ws[f'A{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'B{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'C{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'D{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'E{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'F{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'G{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws[f'H{row}'].alignment = Alignment(horizontal="center", vertical="center")
            ws.cell(row, 1).value = x["serial"]
            ws.cell(row, 2).value = x["company_name"]
            ws.cell(row, 3).value = x["participate_number"]
            ws.cell(row, 4).value = x["best_prople"]
            ws.cell(row, 5).value = x["best_count"]
            ws.cell(row, 6).value = x["good_prople"]
            ws.cell(row, 7).value = x["ordinary_prople"]
            ws.cell(row, 8).value = x["bad_prople"]
            ws.row_dimensions[row].height = 31
            ws[f'A{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'B{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'C{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'D{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'E{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'F{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'G{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            ws[f'H{row}'].border = Border(left=Side(style='thin'), bottom=Side(style='thin'), right=Side(style='thin'), top=Side(style='thin'))
            row += 1
        ws.column_dimensions['A'].width = 10
        ws.column_dimensions['B'].width = 25
        ws.column_dimensions['c'].width = 10
        ws.column_dimensions['D'].width = 50
        ws.column_dimensions['E'].width = 10
        ws.column_dimensions['F'].width = 50
        ws.column_dimensions['G'].width = 50
        ws.column_dimensions['H'].width = 50
        FilePath = os.getcwd() + "/static/medical_ethics/excel_file"
        if not os.path.exists(FilePath):
            os.makedirs(FilePath)
        file_path = os.getcwd() + "/static/medical_ethics/excel_file/{}年医德医风考核公示.xlsx".format(years)
        wb.save(file_path)
        return file_path

